﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text;
using System.Net;
using System.IO;
using System.Data;

/// <summary>
/// Summary description for GoogleMapObject
/// </summary>
public class GoogleMapObject
{
    #region properties
    private string _Width;

    public string Width
    {
        get { return _Width; }
        set 
        {

            _Width = value; 
        }
    }
    private string _Height;

    public string Height
    {
        get { return _Height; }
        set { _Height = value; }
    }

    private string _APIKey;

    public string APIKey
    {
        get { return _APIKey; }
        set { _APIKey = value; }
    }

    private int _ZoomLevel;

    public int ZoomLevel
    {
        get { return _ZoomLevel; }
        set 
        {
            _ZoomLevel = value; 
        }
    }

    private GooglePoint _CenterPoint = new GooglePoint();
    public GooglePoint CenterPoint
    {
        get { return _CenterPoint; }
        set { _CenterPoint = value; }
    }

    private string[] arrayStringMapType = { "roadmap", "satellite", "hybrid", "terrain" };
    public enum GoogleMapType { ROADMAP=0, SATELLITE, HYBRID, TERRAIN };

    private GoogleMapType _MapType;

    public GoogleMapType MapType
    {
        //get { return _MapType; }
        set { _MapType = value; _StringMapType = arrayStringMapType[(int)_MapType]; }
    }

    private string _StringMapType = "roadmap";

    public string StringMapType
    {
        get { return _StringMapType; }
    }

    GooglePoints _Markers = new GooglePoints();
    public GooglePoints Markers
    {
        get { return _Markers; }
        set { _Markers = value; }
    }

    private Boolean _PanControl;//: boolean,

    public Boolean PanControl
    {
        get { return _PanControl; }
        set { _PanControl = value; }
    }
    private Boolean _ZoomControl;//: boolean,

    public Boolean ZoomControl
    {
        get { return _ZoomControl; }
        set { _ZoomControl = value; }
    }
  private Boolean _MapTypeControl;//: boolean,

  public Boolean MapTypeControl
  {
      get { return _MapTypeControl; }
      set { _MapTypeControl = value; }
  }
  private Boolean _ScaleControl;//: boolean,

  public Boolean ScaleControl
  {
      get { return _ScaleControl; }
      set { _ScaleControl = value; }
  }
  private Boolean _StreetViewControl;//: boolean,

  public Boolean StreetViewControl
  {
      get { return _StreetViewControl; }
      set { _StreetViewControl = value; }
  }
  private Boolean _OverviewMapControl;//: boolean

  public Boolean OverviewMapControl
  {
      get { return _OverviewMapControl; }
      set { _OverviewMapControl = value; }
  }

  private int _CountControlsCurrent;

  public int CountControlsCurrent
  {
      get { return _CountControlsCurrent; }
      //set { _CountControlsCurrent = value; }
  }
  private int _CountControlsOld;

  public int CountControlsOld
  {
      get { return _CountControlsOld; }
      //set { _CountControlsOld = value; }
  }

  private List<CustomControl> _Controls = new List<CustomControl>();

  public List<CustomControl> Controls
  {
      get { return _Controls; }
      set { _Controls = value; }
  }

  private int _CountStyleCurrent;

  public int CountStyleCurrent
  {
      get { return _CountStyleCurrent; }
      set { _CountStyleCurrent = value; }
  }
  private int _CountStyleOld;

  public int CountStyleOld
  {
      get { return _CountStyleOld; }
      set { _CountStyleOld = value; }
  }

  private List<Style> _Styles = new List<Style>();

  public List<Style> Styles
  {
      get { return _Styles; }
      set { _Styles = value; }
  }

    #endregion

    #region event-delegate
  public delegate void EventHandlerForGoogleMapMouse(double latitude, double longitude);
  public event EventHandlerForGoogleMapMouse MouseClick;
  public event EventHandlerForGoogleMapMouse MouseDoubleClick;
  public event EventHandlerForGoogleMapMouse MouseUp;
  public event EventHandlerForGoogleMapMouse MouseDown;
  public event EventHandlerForGoogleMapMouse MouseOver;
  public event EventHandlerForGoogleMapMouse MouseOut;

  public delegate void EventHandlerForGoogleMapChanged();
  public event EventHandlerForGoogleMapChanged ZoomChanged;
  public event EventHandlerForGoogleMapChanged CenterChanged;
  public event EventHandlerForGoogleMapChanged BoundChanged;

  public delegate void EventHandlerForMarker(string pID);
  public event EventHandlerForMarker MarkerClicked;
  public event EventHandlerForMarker MarkerDrag;

    #endregion

    #region Handle-functions-event-delegate

    private void OnMouseClick(string parameter)
    {
        if (MouseClick != null)
        {
            double latitude = 0.0;
            double longitude = 0.0;
            if (!string.IsNullOrEmpty(parameter))
            {
                latitude = double.Parse(parameter.Split(',')[0]);
                longitude = double.Parse(parameter.Split(',')[1]);
                MouseClick(latitude, longitude);
            }
            
        }
    }

    private void OnMarkerDrag(string paremeter)
    {
        // Check if there are any Subscribers
        if (MarkerDrag != null)
        {
            // Call the Event
            if (!string.IsNullOrEmpty(paremeter))
            {
                MarkerDrag(paremeter);
            }
        }
    }

    private void OnMarkerClicked(string paremeter)
    {
        // Check if there are any Subscribers
        if (MarkerClicked != null)
        {
            // Call the Event
            if (!string.IsNullOrEmpty(paremeter))
            {
                MarkerClicked(paremeter);
            }
        }
    }

    private void OnMouseDoubleClick(string parameter)
    {
        if (MouseDoubleClick != null)
        {
            double latitude = 0.0;
            double longitude = 0.0;
            if (!string.IsNullOrEmpty(parameter))
            {
                latitude = double.Parse(parameter.Split(',')[0]);
                longitude = double.Parse(parameter.Split(',')[1]);
                MouseDoubleClick(latitude, longitude);
            }
            
        }
    }

    private void OnMouseUp(string parameter)
    {
        if (MouseUp != null)
        {
            double latitude = 0.0;
            double longitude = 0.0;
            if (!string.IsNullOrEmpty(parameter))
            {
                latitude = double.Parse(parameter.Split(',')[0]);
                longitude = double.Parse(parameter.Split(',')[1]);
                MouseUp(latitude, longitude);
            }
            
        }
    }

    private void OnMouseDown(string parameter)
    {
        if (MouseDown != null)
        {
            double latitude = 0.0;
            double longitude = 0.0;
            if (!string.IsNullOrEmpty(parameter))
            {
                latitude = double.Parse(parameter.Split(',')[0]);
                longitude = double.Parse(parameter.Split(',')[1]);
                MouseDown(latitude, longitude);
            }
            
        }
    }

    private void OnMouseOver(string parameter)
    {
        if (MouseOver != null)
        {
            double latitude = 0.0;
            double longitude = 0.0;
            if (!string.IsNullOrEmpty(parameter))
            {
                latitude = double.Parse(parameter.Split(',')[0]);
                longitude = double.Parse(parameter.Split(',')[1]);
                MouseOver(latitude, longitude);
            }
            
        }
    }

    private void OnMouseOut(string parameter)
    {
        if (MouseOut != null)
        {
            double latitude = 0.0;
            double longitude = 0.0;
            if (!string.IsNullOrEmpty(parameter))
            {
                latitude = double.Parse(parameter.Split(',')[0]);
                longitude = double.Parse(parameter.Split(',')[1]);
                MouseOut(latitude, longitude);
            }
            
        }
    }

    private void OnZoomChanged()
        {
            if (ZoomChanged != null)
                ZoomChanged();
        }

    private void OnCenterChanged()
    {
        if (CenterChanged != null)
            CenterChanged();
    }

    private void OnBoundChanged()
    {
        if (BoundChanged != null)
            BoundChanged();
    }
    #endregion

	public GoogleMapObject()
	{
		//
		// TODO: Add constructor logic here
		//
        _ZoomLevel = 16;
        _PanControl = true;
        _ZoomControl = true;//: boolean,


    _MapTypeControl = true;//: boolean,


  _ScaleControl = true;//: boolean,


  _StreetViewControl = true;//: boolean,

  
  

         _OverviewMapControl = true;
   
	}

    public GoogleMapObject(GoogleMapObject prev)
    {
        Markers = GooglePoints.CloneMe(prev.Markers);
        ZoomLevel = prev.ZoomLevel;
        Width = prev.Width;
        Height = prev.Height;
        APIKey = prev.APIKey;
       
    }

    public void OnEventHandlerForGoogleMap(string type_event, string parameter)
    {
        switch(type_event)
        {
            case "MouseClick":
                OnMouseClick(parameter);
                break;
            case "MouseDoubleClick":
                OnMouseDoubleClick(parameter);
                break;
            case "MouseUp":
                OnMouseUp(parameter);
                break;
            case "MouseDown":
                OnMouseDown(parameter);
                break;
            case "MouseOver":
                OnMouseOver(parameter);
                break;
            case "MouseOut":
                OnMouseOut(parameter);
                break;
            case "OnZoomChanged":
                OnZoomChanged();
                break;
            case "ControlClicked":
                ControlClicked(parameter);
                break;
            case "MarkerClicked":
                OnMarkerClicked(parameter);
                break;
            case "MarkerDrag":
                OnMarkerDrag(parameter);
                break;
        }
    }

    private void ControlClicked(string parameter)
    {
        int index = int.Parse(parameter.Trim());
        if (0 <= index && index < Controls.Count)
        {
            Controls[index].OnClicked();
        }
        //for (int i = 0; i < Controls.Count; i++)
        //{
        //    if (parameter.Trim().Equals(i.ToString()))
        //    {
        //        if(Controls[i])
        //            return;
        //    }
        //}
    }

    public void AddControl(CustomControl control)
    {
        _CountControlsOld = _CountControlsCurrent;
        Controls.Add(control);
        _CountControlsCurrent++;
    }

    public void AddStyle(Style style)
    {
        _CountStyleOld = _CountStyleCurrent;
        _Styles.Add(style);
        _CountStyleCurrent++;
    }
}